package com.twu.biblioteca;


import java.lang.reflect.Method;
import java.util.HashMap;

public class Library {

    StringFormatter stringFormatter = new StringFormatter();
    UserHelper userHelper = new UserHelper();

    BookInventory bookInventory = new BookInventory();
    String[] credentials;

    public Library() {
    }

    public String welcomeMessage() {
        return "Welcome! This is BiblioTeca: Your Ultimate Library Guide.";
    }

    public void printOptionsMessage() {
        stringFormatter.printString(optionsMessage());
    }

    public String optionsMessage() {

        return ("Menu Options: Home | Check Book | Membership | View Books |Reserve Book|View Movies " + "\n" +
                "Press 1 for Home, 2 for Membership, 3 to View Books, 4 to Reserve Book, 5 to View Movies and 6 to Exit");
    }

    public String invalidOptionMessage() {
        return "Invalid option selected";
    }

    public String viewBooks() {
        StringBuilder books = new StringBuilder();
        credentials = userHelper.getUserCredentials();

        if (userHelper.isUserCredetialsCorrect(credentials[0], credentials[1])) {
            for (Book book : bookInventory.books) {
                books.append(book + "\n");
            }
        } else {
            books.append("You are not logged in");
        }
        return books.toString();
    }

    public String reserveBook() {

        return bookInventory.isBookAvailable(Integer.parseInt(stringFormatter.getUserInput("Enter Book ID : "))) ? "Thank you ,enjoy the book" : "Book is not available";
    }

    public String checkMemberShip() {

        credentials = userHelper.getUserCredentials();

        if (userHelper.isUserCredetialsCorrect(credentials[0], credentials[1])) {
            return userHelper.getUser(credentials[0]).toString();
        }
        return "Please talk to a Librarian. Thank you";
    }

    public String viewMovies() {
        StringBuilder movies = new StringBuilder();

        credentials = userHelper.getUserCredentials();

        if (userHelper.isUserCredetialsCorrect(credentials[0], credentials[1])) {
            for (Movie movie : new MovieInventory().movies) {
                movies.append(movie + "\n");
            }
        } else {

            movies.append("You are not logged in");
        }

        return movies.toString();
    }

    public void startBibliotecaApp() {
        stringFormatter.printString(welcomeMessage());
        printOptionsMessage();

        getMethodName();
    }

    private void getMethodName() {
        Menu[] menuItems = Menu.values();
        interpretOptions(menuItems[selectedOption()].toString());
    }

    private int selectedOption() {
        return stringFormatter.verifySelectedOption(userInput());
    }

    private String userInput() {
        return stringFormatter.getUserInput("Enter here : ");
    }

    private void interpretOptions(String methodName) {
        try {
            Class<Library> classInstance = Library.class;
            Object obj = new Library();

            Method method = classInstance.getMethod(methodName);

            stringFormatter.printString((String) method.invoke(obj));

            displayOptionsForUser();


        } catch (Exception e) {

        }
    }

    private void displayOptionsForUser() {
        printOptionsMessage();
        getMethodName();
    }

    private void stopBibliotecaApp() {
        System.exit(0);
    }
}
